package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询所有护理等级
     *
     * @return
     */
    @Override
    public NursingLevel[] listAll() {
        NursingLevel[] nursingLevels = nursingLevelMapper.listAll();
        return nursingLevels;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevelDto
     */
    @Override
    public void addNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        nursingLevelMapper.addNursingLevel(nursingLevel);
    }

    /**
     * 条件分页查询
     *
     * @param name     护理等级名称
     * @param pageNum  当前页码
     * @param pageSize 当前页码
     * @param status   状态
     * @return 护理等级集合
     */
    @Override
    public PageResponse<NursingLevel> list(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevel> nursingLevels = (Page<NursingLevel>) nursingLevelMapper.list(name, status);
        PageResponse<NursingLevel> pageResponse = new PageResponse<>();
        pageResponse.setTotal(nursingLevels.getTotal());
        pageResponse.setPage(pageNum);
        pageResponse.setPageSize(pageSize);
        pageResponse.setRecords(nursingLevels.getResult());
        pageResponse.setPages((long) nursingLevels.getPages());
        return pageResponse;
    }

    /**
     * 根据id查询护理等级
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo findById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.findById(id);
        NursingLevelVo nursingLevelVo = new NursingLevelVo();
        if (nursingLevel != null) {
            BeanUtils.copyProperties(nursingLevel, nursingLevelVo);
        }
        return nursingLevelVo;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevelDto 护理等级Dto
     */
    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 删除
     *
     * @param id id
     */
    @Override
    public void delete(Long id) {
        nursingLevelMapper.delete(id);
    }

    /**
     * 启用或禁用
     *
     * @param id     id
     * @param status 状态
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        if (status == 1) {
            nursingLevelMapper.updateStatus(id, 0);
        } else if (status == 0) {
            nursingLevelMapper.updateStatus(id, 1);
        }
    }
}
